package io.wongxd.solution.compose.composeTheme

import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density

private val LightColorPalette = lightColors(
    primary = PrimaryColorLight,
    primaryVariant = PrimaryVariantColorLight,
    secondary = DivideColorLight,
    background = BackgroundColorLight,
    surface = SurfaceColorLight
)

@Composable
fun AppTheme(
    appTheme: AppTheme = AppThemeSwitcher.currentTheme,
    colors: Colors = MaterialTheme.colors,
    typography: Typography = MaterialTheme.typography,
    shapes: Shapes = MaterialTheme.shapes,
    content: @Composable () -> Unit
) {

//    val colors = when (appTheme) {
//        AppTheme.Light -> {
//            LightColorPalette
//        }
//        AppTheme.Dark -> {
//            LightColorPalette
//        }
//    }
//
//    val typography = if (appTheme.isDarkTheme()) {
//        LightTypography
//    } else {
//        LightTypography
//    }

//    val themeType = remember {
//        AppCompatDelegate.getDefaultNightMode()
//    }

//    var colors = LightColorPalette
//    if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
//        // 关闭暗黑模式
////        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
//    } else {
//        colors = LightColorPalette
//        // 开启暗黑模式
////        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
//    }

    val fontScale = LocalDensity.current.fontScale
    val displayMetrics = LocalContext.current.resources.displayMetrics
    val widthPixels = displayMetrics.widthPixels
    MaterialTheme(
        colors = colors,
        typography = typography,
        shapes = shapes,
        content = {
            CompositionLocalProvider(
                LocalDensity provides Density(
                    density = widthPixels / 375.0f,
                    fontScale = fontScale
                )
            ) {
                content()
            }
        }
    )
}

@Composable
fun PreviewTheme(
    content: @Composable () -> Unit
) {
    MaterialTheme(content = content)
}